package com.Xht.www.servlet;

//后端响应取消收藏商家的请求

import com.Xht.www.dao.MerchantInfoDAO;
import com.Xht.www.dao.UserFavoritesDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/unfavorite")
public class UnfavoriteMerchantServlet extends HttpServlet {
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String favoriteIdParam = request.getParameter("favoriteId");
        if (favoriteIdParam == null || favoriteIdParam.isEmpty()) {
            out.write("{\"success\": false, \"message\": \"收藏ID不能为空\"}");
            return;
        }
        try {
            int favoriteId = Integer.parseInt(favoriteIdParam);
            UserFavoritesDAO dao = new UserFavoritesDAO();
            boolean result = dao.deleteUserFavorites(favoriteId);
            if (result) {
                out.write("{\"success\": true, \"message\": \"取消收藏成功\"}");
            } else {
                out.write("{\"success\": false, \"message\": \"取消收藏失败\"}");
            }
        } catch (NumberFormatException e) {
            out.write("{\"success\": false, \"message\": \"收藏ID格式错误\"}");
        } catch (Exception e) {
            e.printStackTrace();
            out.write("{\"success\": false, \"message\": \"服务器内部错误\"}");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String merchantIdParam = request.getParameter("merchantId");
        String changeParam = request.getParameter("change");

        if (merchantIdParam == null || merchantIdParam.isEmpty() || changeParam == null || changeParam.isEmpty()) {
            out.write("{\"success\": false, \"message\": \"商家ID或变化量不能为空\"}");
            return;
        }

        try {
            int merchantId = Integer.parseInt(merchantIdParam);
            int change = Integer.parseInt(changeParam);
            MerchantInfoDAO dao = new MerchantInfoDAO();
            boolean result = dao.updateFavoritesCount(merchantId, change);
            if (result) {
                out.write("{\"success\": true, \"message\": \"更新收藏量成功\"}");
            } else {
                out.write("{\"success\": false, \"message\": \"更新收藏量失败\"}");
            }
        } catch (NumberFormatException e) {
            out.write("{\"success\": false, \"message\": \"商家ID或变化量格式错误\"}");
        } catch (Exception e) {
            e.printStackTrace();
            out.write("{\"success\": false, \"message\": \"服务器内部错误\"}");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String merchantName = request.getParameter("merchantName");

        if (merchantName == null || merchantName.isEmpty()) {
            out.write("{\"success\": false, \"message\": \"商家名称不能为空\"}");
            return;
        }

        try {
            MerchantInfoDAO dao = new MerchantInfoDAO();
            Integer merchantId = dao.getMerchantIdByName(merchantName);
            if (merchantId != null) {
                out.write("{\"success\": true, \"merchantId\": " + merchantId + "}");
            } else {
                out.write("{\"success\": false, \"message\": \"未找到该商家\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            out.write("{\"success\": false, \"message\": \"服务器内部错误\"}");
        }
    }


}
